package org.example.wxdemo.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XmlUtils {
    private static final Logger logger = LoggerFactory.getLogger(XmlUtils.class);
    private static final XmlMapper xmlMapper = new XmlMapper();

    public static <T> T fromXml(String xml, Class<T> valueType) {
        try {
            return xmlMapper.readValue(xml, valueType);
        } catch (JsonProcessingException e) {
            logger.error("Error deserializing XML to object", e);
            throw new RuntimeException("Failed to parse XML", e);
        }
    }

    public static String toXml(Object value) {
        try {
            return xmlMapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            logger.error("Error serializing object to XML", e);
            throw new RuntimeException("Failed to generate XML", e);
        }
    }
} 